<template>
  <div class="app-container">
    <!-- 用户介绍 -->
    <el-card class="box-card">
      <h3>新增用户</h3>
      <el-form
        ref="user"
        :rules="rules"
        :model="user"
        label-width="150px"
      >
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="user.userId"/>
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="user.userName"/>
        </el-form-item>

        <el-form-item label="用户性别">
          <el-select v-model="user.userSex">
            <el-option
              v-for="(item, index) in genders"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户权限">
          <el-select v-model="user.userSex">
            <el-option
              v-for="(item, index) in genders"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户图片">
          <el-upload
            ref="upload"
            :action="uploadPath"
            :before-upload="handleChangeFile"
            :show-file-list="false"
            list-type="picture"
            :headers="headers"
            :on-success="uploadPicUrl"
            class="avatar-uploader"
            accept=".jpg,.jpeg,.png,.gif"
            :auto-upload="true"
            :multiple="true"
          >
            <img
              v-if="user.userImg"
              :src="user.userImg"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>

        <el-form-item label="用户备注">
          <el-input v-model="user.remarks"/>
        </el-form-item>


      </el-form>
    </el-card>

    <!-- 页面底部用户取消按钮 -->
    <div class="op-container">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handlePublish">新增</el-button>
    </div>
  </div>
</template>

<style>
.el-card {
  margin-bottom: 10px;
}

.el-tag + .el-tag {
  margin-left: 10px;
}

.input-new-keyword {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.avatar-uploader .el-upload {
  width: 145px;
  height: 145px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
</style>

<script>
import {addUser} from "@/api/system/user";
import {createStorage, uploadPath} from "@/api/system/storage";
import TinymceEditor from "@/components/TEditor.vue";
import {MessageBox} from "element-ui";
import {getToken} from "@/utils/auth";

export default {
  name: "UserCreate",
  components: {Editor: TinymceEditor},

  data() {
    return {
      fileList: [],
      genders: [
        {label: "男", value: "男"},
        {label: "女", value: "女"}
      ],
      uploadPath: process.env.VUE_APP_TARGET_API + "/upload", // 文件上传路径
      user: {userImg: ""}, // 用户信息
      // 验证规则
      rules: {
        userAddress: [
          {required: true, message: "用户地址不能为空", trigger: "blur"},
        ],
        userName: [
          {required: true, message: "用户名称不能为空", trigger: "blur"},
        ],
      },

    };
  },
  computed: {
    // 上传文件请求头添加Token
    headers() {
      return {
        "Shopping-Admin-Token": getToken(),
      };
    },
  },
  created() {
    // 页面数据初始化
    this.init();
  },

  methods: {
    handleChangeFile(file, fileList) {
      if (!file) return;
      // suffix：后缀名 ，如：hello.jpg
      var suffix = file.name.substring(file.name.lastIndexOf(".") + 1);
      const limit = file.size / 1024 / 1024 < 1;
      if (suffix !== "jpg" && suffix !== "png" && suffix !== "bmp") {
        this.$refs.upload.clearFiles();
        this.$message({
          message: "上传文件只能是 .jpg、.png、bmp格式!",
          type: "warning",
        });
        return false;
      }
      if (!limit) {
        this.$refs.upload.clearFiles();
        this.$message({
          message: "上传文件大小必须小于1MB!",
          type: "warning",
        });
        return false;
      }
    },
    // 获取数据
    init: function () {
      /* listCatAndBrand().then((response) => {
        this.categoryList = response.data.data.categoryList;
        this.brandList = response.data.data.brandList;
      }); */
    },

    // 取消用户上架钩子
    handleCancel: function () {
      this.$router.push({path: "/system/user/list"});
    },
    // 上架用户 钩子
    handlePublish: function () {
      // 调用接口
      addUser(this.user)
        .then((response) => {
          this.$notify.success({
            title: "成功",
            message: response.msg,
          });
          this.$router.push({path: "/system/user/list"});
        })
        .catch((response) => {
          MessageBox.alert("业务错误：" + response.data.errmsg, "警告", {
            confirmButtonText: "确定",
            type: "error",
          });
        });
    },

    // 用户图片上传
    uploadPicUrl: function (response) {
      let fm = process.env.VUE_APP_IMAGE_SHOW_FORM;
      console.log("----新增用户：fm:" + fm);
      if ("db" == fm) {
        this.user.userImg = response.data.url;
      } else if ("file" == fm) {
        this.user.userImg =
          process.env.VUE_APP_TARGET_API + response.data.url;
      }
    }
  }
};
</script>
